package GUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.border.LineBorder;

import Classes.Login;
import Repositorio.Rep_Util;

import java.awt.Color;

public class JanelaLogin extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtEmail;
	private JPasswordField tpassSenha;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			JanelaLogin dialog = new JanelaLogin();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public JanelaLogin() {
		setBounds(100, 100, 268, 196);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(new Color(176, 196, 222));
		contentPanel.setForeground(new Color(100, 149, 237));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(176, 196, 222));
		panel.setForeground(new Color(51, 204, 255));
		panel.setBorder(new LineBorder(Color.WHITE));
		panel.setBounds(10, 11, 230, 102);
		contentPanel.add(panel);
		panel.setLayout(null);
		
		txtEmail = new JTextField();
		txtEmail.setColumns(10);
		txtEmail.setBounds(95, 11, 109, 20);
		panel.add(txtEmail);
		
		tpassSenha = new JPasswordField();
		tpassSenha.setBounds(95, 42, 109, 20);
		panel.add(tpassSenha);
		
		final JComboBox cmbTipo = new JComboBox();
		cmbTipo.setModel(new DefaultComboBoxModel(new String[] {"Aluno", "Professor", "Secretario"}));
		cmbTipo.setBounds(95, 73, 109, 20);
		panel.add(cmbTipo);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setBounds(10, 14, 46, 14);
		panel.add(lblEmail);
		
		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setBounds(10, 45, 46, 14);
		panel.add(lblSenha);
		
		JLabel lblTipoAcesso = new JLabel("Tipo Acesso:");
		lblTipoAcesso.setBounds(10, 76, 75, 14);
		panel.add(lblTipoAcesso);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(new Color(230, 230, 250));
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) 
					{
						String nome = "";
						Rep_Util rep = new Rep_Util();
						Login login = new Login();
					
						if (txtEmail.getText() == "" || tpassSenha.getText() == "")
						{
							JOptionPane.showMessageDialog(null, "Favor preencher Campos");
							txtEmail.setFocusable(true);
						}
						
						else if (cmbTipo.getSelectedIndex() == 0)
						{
							TelaListaAluno l_aluno = new TelaListaAluno();
							l_aluno.CarregaTabela(txtEmail.getText());
							l_aluno.setVisible(true);
						}
						
						else if (cmbTipo.getSelectedIndex() == 1)
						{
							nome = rep.AcessoProfessor(txtEmail.getText(), tpassSenha.getText());
							if (nome.toString() != "")
							{
								login.setNome(nome);
								login.setTipo_acesso("P");
								JOptionPane.showMessageDialog(null, "Login Ok - Professor: " +login.getNome());
							}
							else
							{
								JOptionPane.showMessageDialog(null, "Login erro" +login.getNome());
							}
						
						}
						else if (cmbTipo.getSelectedIndex() == 2)
						{
							nome = rep.AcessoSecretario(txtEmail.getText(), tpassSenha.getText());
							if (nome.toString() != "")
							{
								login.setNome(nome);
								login.setTipo_acesso("S");
								JOptionPane.showMessageDialog(null, "Login Ok - Secretario: " +login.getNome());
							}
							else
							{
								JOptionPane.showMessageDialog(null, "Login erro" +login.getNome());
							}
						}
						
					}		
					});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
